package com.jinlejia.erp.ui.customer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseIntArray;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityRecordCustomer2Binding;
import com.jinlejia.erp.ui.common.MultiSheetSelect;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.StringUtil;
import com.weight.CustomToast;
import com.weight.IconToast;
import com.zhy.http.okhttp.OkHttpUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 录客源 第二步
 */
public class RecordCustomer2Activity extends AppCompatActivity {

    JSONObject jsonObject = new JSONObject();

    private String orderCounty1, orderArea1, orderCounty2, orderArea2, payType1, payType2, houseType1, houseType2, face1, face2, fit1, fit2, id1, id2;
    private ArrayList<Map<String, Object>> payType = new ArrayList<>();
    private ArrayList<Map<String, Object>> houseType = new ArrayList<>();
    private ArrayList<Map<String, Object>> face = new ArrayList<>();
    private ArrayList<Map<String, Object>> fit = new ArrayList<>();
    private ArrayList<String> estateList = new ArrayList<>();
    private ArrayList<String> insertList = new ArrayList<>();
    private ArrayList<String> delList = new ArrayList<>();

    private String customerType;
    private ProgressDialog progressDialog;
    private ActivityRecordCustomer2Binding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_record_customer2);
        binding.setEvent(this);
        customerType = getIntent().getStringExtra("type");
        initView();
        EventBus.getDefault().registerSticky(this);
        progressDialog = new ProgressDialog(this, "正在提交...");
        EventBus.getDefault().postSticky("666","TAG_NEW_CUSTOMER");
    }

    private void initView() {
        binding.remark.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = 200 - s.length();
                binding.remarkCount.setText(number + "/200");
                selectionStart = binding.remark.getSelectionStart();
                selectionEnd = binding.remark.getSelectionEnd();
                if (temp.length() > 200) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    binding.remark.setText(s);
                    binding.remark.setSelection(tempSelection);//设置光标在最后
                }
            }
        });


    }

    private SparseIntArray layout2Edit;

    public void onClickLayoutToFocusEditText(View view){
        if (layout2Edit==null){
            layout2Edit = new SparseIntArray();
            layout2Edit.put(R.id.priceMinLayout,R.id.etPriceMin); //最低价格
            layout2Edit.put(R.id.priceMaxLayout,R.id.etPriceMax); //最高价格
            layout2Edit.put(R.id.areaMaxLayout,R.id.etAreaMax); //最大面积
            layout2Edit.put(R.id.areaMinLayout,R.id.etAreaMin); //最小面积
            layout2Edit.put(R.id.priceMinLayout2,R.id.etPriceMin2);//需求二最低价格
            layout2Edit.put(R.id.priceMaxLayout2,R.id.etPriceMax2); //需求二最低价格
            layout2Edit.put(R.id.areaMaxLayout2,R.id.etAreaMax2); //需求二最大面积
            layout2Edit.put(R.id.areaMinLayout2,R.id.etAreaMin2); //需求二最小面积
        }
        int value = layout2Edit.get(view.getId(),-1);
        if (value==-1)return;
        EditText et = findViewById(value);
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm==null)return;
        imm.showSoftInput(et, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btBack:
                finish();
                break;
            case R.id.btAdd:
                if (!binding.need2.isShown()) {
                    binding.need2.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(RecordCustomer2Activity.this, "仅限添加两条购房需求", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btPre:
                //上一步
                finish();
                break;
            case R.id.btSubmit:
                //提交
                putData();
                break;
            case R.id.tvLocation:
                //位置
                Intent lIntent = new Intent(this, LocationActivity.class);
                lIntent.putExtra("no_limit", true);
                lIntent.putExtra("county",orderCounty1);
                lIntent.putExtra("area",orderArea1);
                startActivityForResult(lIntent, 101);
                break;
            case R.id.tvRooms:
                final MultiSheetSelect select = new MultiSheetSelect(this, "居室", houseType,str2list(houseType1));
                select.show();
                select.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        binding.tvRooms.setText(name);
                        houseType1 = value;
                    }
                });
                break;
            case R.id.tvPayWay:
                //付款方式
                final MultiSheetSelect payWay = new MultiSheetSelect(this, "付款方式", payType,str2list(payType1));
                payWay.show();
                payWay.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        binding.tvPayWay.setText(name);
                        payType1 = value;
                    }
                });
                break;
            case R.id.tvZhuangxiu:
                //装修
                final MultiSheetSelect fitSheet = new MultiSheetSelect(this, "装修", fit,str2list(fit1));
                fitSheet.show();
                fitSheet.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        binding.tvZhuangxiu.setText(name);
                        fit1 = value;
                    }
                });
                break;
            case R.id.tvOrientation:
                //朝向
                final MultiSheetSelect faceSheet = new MultiSheetSelect(this, "朝向", face,str2list(face1));
                faceSheet.show();
                faceSheet.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        binding.tvOrientation.setText(name);
                        face1 = value;
                    }
                });
                break;
            case R.id.tvLocation2:
                Intent lIntent2 = new Intent(this, LocationActivity.class);
                lIntent2.putExtra("no_limit", true);
                lIntent2.putExtra("county",orderCounty2);
                lIntent2.putExtra("area",orderArea2);
                startActivityForResult(lIntent2, 102);
                break;
            case R.id.tvRooms2:
                final MultiSheetSelect select2 = new MultiSheetSelect(this, "居室", houseType,str2list(houseType2));
                select2.show();
                select2.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        binding.tvRooms2.setText(name);
                        houseType2 = value;
                    }
                });
                break;
            case R.id.tvPayWay2:
                //付款方式
                final MultiSheetSelect payWay2 = new MultiSheetSelect(this, "付款方式", payType,str2list(payType2));
                payWay2.show();
                payWay2.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        payWay2.dismiss();
                        binding.tvPayWay2.setText(name);
                        payType2 = value;
                    }
                });
                break;
            case R.id.tvZhuangxiu2:
                //装修
                final MultiSheetSelect fitSheet2 = new MultiSheetSelect(this, "装修", fit,str2list(fit2));
                fitSheet2.show();
                fitSheet2.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        fitSheet2.dismiss();
                        binding.tvZhuangxiu2.setText(name);
                        fit2 = value;
                    }
                });
                break;
            case R.id.tvOrientation2:
                //朝向
                final MultiSheetSelect faceSheet2 = new MultiSheetSelect(this, "朝向", face,str2list(face2));
                faceSheet2.show();
                faceSheet2.setOnSelectListener(new MultiSheetSelect.OnSelectListener() {
                    @Override
                    public void select(String name, String value) {
                        faceSheet2.dismiss();
                        binding.tvOrientation2.setText(name);
                        face2 = value;
                    }
                });
                break;
            case R.id.tvInterest:
                //兴趣小区
                Intent intent = new Intent(RecordCustomer2Activity.this,RecordCusromerInterestActivity.class);
                intent.putExtra("type",customerType);
                intent.putExtra("estateList",estateList);
                startActivityForResult( intent,103 );

                break;
            default:
                break;
        }
    }
    private List<String> str2list(String str ){
        if( TextUtils.isEmpty(str) )
            str="";
        return Arrays.asList(str.split(";"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 101:
                    //意向1位置
                    orderCounty1 = data.getStringExtra("countyName");
                    orderArea1 = data.getStringExtra("districtname");
                    if (!TextUtils.isEmpty(orderCounty1))
                        binding.tvLocation.setText(orderCounty1 + " - " + orderArea1);
                    break;
                case 102:
                    //意向2位置
                    orderCounty2 = data.getStringExtra("countyName");
                    orderArea2 = data.getStringExtra("districtname");
                    if (!TextUtils.isEmpty(orderCounty2))
                        binding.tvLocation2.setText(orderCounty2 + " - " + orderArea2);
                    break;
                case 103:
                    //兴趣小区
                    estateList = data.getStringArrayListExtra("data");
                    insertList = data.getStringArrayListExtra("insert");
                    delList = data.getStringArrayListExtra("del");
                    binding.tvInterest.setText( estateList.toString().replace("[","").replace("]","") );
                    break;
                default:
                    break;
            }
        }
    }

    @Subscriber(tag = "TAG_RC_STEP1")
    private void step1Data(JSONObject object) {
        EventBus.getDefault().removeStickyEvent(object.getClass(), "TAG_RC_STEP1");
        jsonObject = object;
        System.out.println("ZP =TAG_RC_STEP1==> " + jsonObject.toString());
        if (jsonObject.get("type").toString().equals("1")) {
            binding.tvPriceMaxW.setText("万");
            binding.tvPriceMinW.setText("万");
            binding.tvPriceMinW2.setText("万");
            binding.tvPriceMaxW2.setText("万");
        } else {
            binding.tvPriceMaxW.setText("元");
            binding.tvPriceMinW.setText("元");
            binding.tvPriceMinW2.setText("元");
            binding.tvPriceMaxW2.setText("元");
        }
        getDict();
    }

    @Subscriber(tag = "TAG_EDIT_STEP1")
    private void step1EditData(JSONObject object) {
        EventBus.getDefault().removeStickyEvent(object.getClass(), "TAG_EDIT_STEP1");
        jsonObject = object;
        System.out.println("ZP =TAG_EDIT_STEP1==> " + jsonObject.toString());
        if (customerType.equals("sale")) {
            binding.tvPriceMin.setText("最低价格(万):");
            binding.tvPriceMax.setText("最高价格(万):");
            binding.tvPriceMin2.setText("最低价格(万):");
            binding.tvPriceMax2.setText("最高价格(万):");
        } else {
            binding.tvPriceMin.setText("最低价格(元):");
            binding.tvPriceMax.setText("最高价格(元):");
            binding.tvPriceMin2.setText("最低价格(元):");
            binding.tvPriceMax2.setText("最高价格(元):");
        }

        if (jsonObject.containsKey("customerContactTypeList"))
            jsonObject.put("customerContactTypeList", jsonObject.getJSONArray("customerContactTypeList").toString());
        if (jsonObject.containsKey("entrustTime"))
            jsonObject.remove("entrustTime");
        if (jsonObject.containsKey("customerFollow"))
            jsonObject.remove("customerFollow");
        if (jsonObject.containsKey("isAttention"))
        jsonObject.remove("isAttention");
        if (jsonObject.containsKey("type"))
        jsonObject.remove("type");
        if (jsonObject.containsKey("belongUserName"))
        jsonObject.remove("belongUserName");
        if (jsonObject.containsKey("belongDept"))
        jsonObject.remove("belongDept");

        JSONArray jsonArray = jsonObject.getJSONArray("customerOrderList");
        JSONObject object1 = (JSONObject) jsonArray.get(0);
        binding.tvLocation.setText(object1.getString("orderCounty") + " - " + object1.getString("orderArea"));
        id1 = object1.get("id").toString();
        orderCounty1 = object1.getString("orderCounty");
        orderArea1 = object1.getString("orderArea");
        binding.etPriceMin.setText(object1.get("orderPriceMin").toString());
        binding.etPriceMax.setText(object1.get("orderPriceMax").toString());
        binding.etAreaMin.setText(object1.get("orderSquareMin").toString());
        binding.etAreaMax.setText(object1.get("orderSquareMax").toString());
        binding.tvRooms.setText(object1.get("orderHouseType").toString().replaceAll(";","居;").concat("居"));
        houseType1 = object1.get("orderHouseType").toString();
        if( object1.containsKey("orderPayType") ) {
            binding.tvPayWay.setText(object1.get("orderPayType").toString());
            payType1 = object1.get("orderPayType").toString();
        }else if( object1.containsKey("payType") ) {
            binding.tvPayWay.setText(object1.get("payType").toString());
            payType1 = object1.get("payType").toString();
        }
        if (!"null".equalsIgnoreCase(object1.getString("needRemark"))){
            binding.demandNote.setText(object1.getString("needRemark"));
        }
        binding.tvZhuangxiu.setText(object1.get("orderFit").toString());
        fit1 = object1.get("orderFit").toString();
        binding.tvOrientation.setText(object1.get("orderFace").toString());
        face1 = object1.get("orderFace").toString();
        binding.remark.setText(jsonObject.getString("coreRemark"));
        if (jsonArray.size() > 1) {
            binding.need2.setVisibility(View.VISIBLE);
            JSONObject object2 = (JSONObject) jsonArray.get(1);
            binding.tvLocation2.setText(object2.getString("orderCounty") + " - " + object2.getString("orderArea"));
            id2 = object2.get("id").toString();
            orderCounty2 = object2.getString("orderCounty");
            orderArea2 = object2.getString("orderArea");
            binding.etPriceMin2.setText(object2.get("orderPriceMin").toString());
            binding.etPriceMax2.setText(object2.get("orderPriceMax").toString());
            binding.etAreaMin2.setText(object2.get("orderSquareMin").toString());
            binding.etAreaMax2.setText(object2.get("orderSquareMax").toString());
            binding.tvRooms2.setText(object2.get("orderHouseType").toString().replaceAll(";","居;").concat(" 居"));
            houseType2 = object2.get("orderHouseType").toString();
            if( object2.containsKey("orderPayType") ) {
                binding.tvPayWay2.setText(object2.get("orderPayType").toString());
                payType2 = object2.get("orderPayType").toString();
            }else if( object2.containsKey("payType") ) {
                binding.tvPayWay2.setText(object2.get("payType").toString());
                payType2 = object2.get("payType").toString();
            }
            if (!"null".equalsIgnoreCase(object2.getString("needRemark"))){
                binding.demandNote2.setText(object2.getString("needRemark"));
            }
            binding.tvZhuangxiu2.setText(object2.get("orderFit").toString());
            fit2 = object2.get("orderFit").toString();
            binding.tvOrientation2.setText(object2.get("orderFace").toString());
            face2 = object2.get("orderFace").toString();
        }
        if( jsonObject.containsKey("estateName") ) {
            estateList.addAll(JSON.parseArray(jsonObject.get("estateName").toString(), String.class));
            binding.tvInterest.setText(estateList.toString().replace("[", "").replace("]", ""));
        }
        getDict();
    }

    private void putData() {
        JSONArray customerOrderList = new JSONArray();
        JSONObject object1 = new JSONObject();
        JSONObject object2 = new JSONObject();
        object1.put("orderCounty", orderCounty1);
        object1.put("orderArea", orderArea1);
        object1.put("orderPriceMin", binding.etPriceMin.getText().toString());
        object1.put("orderPriceMax", binding.etPriceMax.getText().toString());
        object1.put("orderSquareMin", binding.etAreaMin.getText().toString());
        object1.put("orderSquareMax", binding.etAreaMax.getText().toString());
        object1.put("orderHouseType", houseType1);
        object1.put("orderPayType", payType1);
        object1.put("orderFit", fit1);
        object1.put("orderFace", face1);
        object1.put("type", 1);
        String demandNote = String.valueOf(binding.demandNote.getText()).trim();
        if (!TextUtils.isEmpty(demandNote)){
            object1.put("needRemark",demandNote);
        }

        if (!TextUtils.isEmpty(id1))
            object1.put("id", id1);
        customerOrderList.add(object1);
        if (binding.need2.isShown()) {
            object2.put("orderCounty", orderCounty2);
            object2.put("orderArea", orderArea2);
            object2.put("orderPriceMin", binding.etPriceMin2.getText().toString());
            object2.put("orderPriceMax", binding.etPriceMax2.getText().toString());
            object2.put("orderSquareMin", binding.etAreaMin2.getText().toString());
            object2.put("orderSquareMax", binding.etAreaMax2.getText().toString());
            object2.put("orderHouseType", houseType2);
            object2.put("orderPayType", payType2);
            object2.put("orderFit", fit2);
            object2.put("orderFace", face2);
            object2.put("type", 0);
            String demandNote2 = String.valueOf(binding.demandNote2.getText()).trim();
            if (!TextUtils.isEmpty(demandNote2)){
                object2.put("needRemark",demandNote2); //需求备注2
            }
            if (!TextUtils.isEmpty(id2))
                object2.put("id", id2);
            customerOrderList.add(object2);
        }
        jsonObject.put("customerOrderList", customerOrderList.toString());
        jsonObject.put("coreRemark", binding.remark.getText().toString());

        if( estateList.size()==0 && TextUtils.isEmpty(customerType) ){
            //录客源兴趣小区必填
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择兴趣小区", 1500);
        }else if (TextUtils.isEmpty(object1.getString("orderCounty"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择位置", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderPriceMin"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入最低价格", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderPriceMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入最高价格", 1500);
        } else if (Long.parseLong(object1.getString("orderPriceMin")) >= Long.parseLong(object1.getString("orderPriceMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "最低价格必须小于最高价格", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderSquareMin"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入最小面积", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderSquareMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入最大 面积", 1500);
        } else if (Long.parseLong(object1.getString("orderSquareMin")) >= Long.parseLong(object1.getString("orderSquareMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "最小面积必须小于最大面积", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderHouseType"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择居室", 1500);
        } else if (TextUtils.isEmpty(object1.getString("orderPayType"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择付款方式", 1500);
        }
//        else if (TextUtils.isEmpty(jsonObject.getString("coreRemark"))) {
//            CustomToast.showToast(RecordCustomer2Activity.this, "请输入核心备注", 1500);
//        }
        else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderCounty"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择需求2位置", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderPriceMin"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入需求2最低价格", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderPriceMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入需求2最高价格", 1500);
        } else if (binding.need2.isShown() && Integer.parseInt(object2.getString("orderPriceMin")) >= Integer.parseInt(object2.getString("orderPriceMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "需求2最高价格必须大于最低价格", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderSquareMin"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入需求2最小面积", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderSquareMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请输入需求2最大面积", 1500);
        } else if (binding.need2.isShown() && Integer.parseInt(object2.getString("orderSquareMin")) >= Integer.parseInt(object2.getString("orderSquareMax"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "需求2最大面积必须大于最小面积", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderHouseType"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择需求2居室", 1500);
        } else if (binding.need2.isShown() && TextUtils.isEmpty(object2.getString("orderPayType"))) {
            CustomToast.showToast(RecordCustomer2Activity.this, "请选择需求2付款方式", 1500);
        } else {
            if (TextUtils.isEmpty(customerType)) {
                jsonObject.put("enjoyEstateList",JSON.toJSONString(estateList));
                if (jsonObject.get("type").toString().equals("0")) {
                    submit(ApiConfig.SAVE_RENT_CUSTOMER);
                } else {
                    submit(ApiConfig.SAVE_SHS_CUSTOMER);
                }
            } else if (customerType.equals("sale") || customerType.equals("shs") ) {
                //买卖编辑
                if( delList.size()>0 )
                jsonObject.put("estateDelList",JSON.toJSONString(delList));
                if(insertList.size()>0)
                jsonObject.put("estateInsertList",JSON.toJSONString(insertList));
                submit(ApiConfig.EDIT_BUY_CUSTOMER);
            } else {
                //租赁编辑
                if( delList.size()>0 )
                jsonObject.put("estateDelList",JSON.toJSONString(delList));
                if(insertList.size()>0)
                jsonObject.put("estateInsertList",JSON.toJSONString(insertList));
                submit(ApiConfig.EDIT_ZU_CUSTOMER);
            }
        }
    }

    private void getDict() {
        String url;
        if (TextUtils.isEmpty(customerType)) {
            if (jsonObject.get("type").toString().equals("0")) {
                url = ApiConfig.CUSTOMER_RENT_DICT;
            } else {
                url = ApiConfig.CUSTOMER_DICT;
            }
        } else if (customerType.equals("sale") || customerType.equals("shs") ) {
            //买卖编辑
            url = ApiConfig.CUSTOMER_DICT;
        } else {
            //租赁编辑
            url = ApiConfig.CUSTOMER_RENT_DICT;
        }
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(RecordCustomer2Activity.this, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            payType = JSON.parseObject(data.get("payType").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            face = JSON.parseObject(data.get("face").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            houseType = JSON.parseObject(data.get("houseType").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            fit = JSON.parseObject(data.get("fit").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                        }
                    }
                });
    }

    private void submit(String url) {
        //删掉回显的兴趣小区
        jsonObject.remove("estateName");
        progressDialog.show();
        OkHttpUtils.postString()
                .url(url)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (DataFilter.filter(RecordCustomer2Activity.this, response)) {

                            IconToast.showToast(RecordCustomer2Activity.this, R.mipmap.common_ic_success, "保存成功", 1500);
                            Intent data = new Intent();
                            data.putExtra("code", StringUtil.valueOf(response.get("data")));
                            data.putExtra("name", jsonObject.getString("name"));
                            data.putExtra("tel", jsonObject.getString("tel"));
                            setResult(RESULT_OK,data);
                            finish();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
